// pages/ucenter/address/address.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show:false,
        slideButtons:[
            {
                text: '删除',
                extClass: 'test',
                src:'/images/ic_delete.png'
            }
        ],
        addressList:[],
        address:{}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let userInfo = wx.getStorageSync('userInfo');

        if (!userInfo){
            wx.showModal({
                title:'提示',
                content:'您还没有登录',
                success(res){
                    if (res.confirm){
                        // wx.switchTab({
                        //   url: '/pages/ucenter/index/index',
                        // })
                        app.getuserInfo();
                    } else if (res.cancel){
                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    }
                }
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let addresslist = wx.getStorageSync('addressList');
        let userInfo = wx.getStorageSync('userInfo');
        
        this.setData({
            addressList:addresslist
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title:'sharetest',
            path:'/pages/index/index?shareid=' + '54185418'
        }
    },
    /**
     * @param e
     * (选择默认地址事件)
     * @徐志鹏
     * @2021-2-5 
     */
    selected:function(e){
        let index = e.currentTarget.dataset.index;
        let addresslist = this.data.addressList;

        if (this.data.show === true){
            this.setData({
                show:false
            })
        } else {
            if (index === 0)
                return;
            let item = addresslist[0];
            addresslist[0] = addresslist[index];
            addresslist[index] = item;
            wx.setStorageSync('addressList', addresslist)

            wx.navigateBack({
              delta: 1,
            })
        }
    },
    /**
     * 
     * @param {*} e
     * (更改加地址事件)
     * @徐志鹏
     * @2021-2-5 
     */
    changeAddress:function(e){
        let index = e.currentTarget.dataset.index;
        if (this.data.show){
            this.setData({
                show:false
            })
        } else {
            wx.navigateTo({
              url: '/pages/ucenter/add_address/add_address?index=' + index,
            })
        }
    },
    /**
     *(增加地址事件)
     * @徐志鹏
     * @2021-2-5 
     */
    addAddress:function(){
        // wx.chooseAddress({
        //     success (res) {
        //       console.log(res.userName)
        //       console.log(res.postalCode)
        //       console.log(res.provinceName)
        //       console.log(res.cityName)
        //       console.log(res.countyName)
        //       console.log(res.detailInfo)
        //       console.log(res.nationalCode)
        //       console.log(res.telNumber)
        //     }
        // })
        if (this.data.show){
            this.setData({
                show:false
            })
        } else {
            wx.navigateTo({
              url: '/pages/ucenter/add_address/add_address',
            })
        }
    },
    /**
     * 
     * @param {*} e
     * (滑动删除) 
     * @徐志鹏
     * @2021-2-5
     */
    bindbuttontap:function(e){
        console.log(this.data.show)
        let that = this;
        let slideButtonsindex = e.detail.index;
        let index = e.currentTarget.dataset.index;
        let addresslist = this.data.addressList;

        wx.showModal({
          content: '确定删除吗？',
          title: '提示',
          success: (result) => {
              if (result.confirm){
                addresslist.splice(index,1);
                that.setData({
                    addressList:addresslist
                })
                wx.setStorageSync('addressList', addresslist);
              } else if (result.cancel){
                  return;
              }
          },
          fail: (res) => {},
          complete: (res) => {},
        })

    },
    /**
     * (监听滑块状态)
     * @徐志鹏
     * @2021-2-5
     */
    bindshow:function(){
        this.setData({
            show:true
        })
    }
})